#include "Subsystems/BlockBehaviors/SubsystemArrowBlockBehavior.h"
#include "GameSingleton.h"
#include "GameRandom.hpp"

#include "Blocks/ArrowBlock.h"

namespace PocketSurvival
{

    void SubsystemArrowBlockBehavior::onFiredAsProjectile(Projectile *projectile)
    {
        if(ArrowBlock::GetArrowType(Terrain::ExtractData(projectile->value)) == ArrowType::FireArrow)
        {
            projectile->projectileStoppedAction = ProjectileStoppedAction::Disappear;
            projectile->isIncendiary = true;
        }
    }

    bool SubsystemArrowBlockBehavior::onHitAsProjectile(std::shared_ptr<CellFace> cellFace, ComponentBody *componentBody, WorldItem *worldItem)
    {
        ArrowType arrowType = ArrowBlock::GetArrowType(Terrain::ExtractData(worldItem->value));
        if (worldItem->velocity.length() > 10.0f)
        {
            float num = 0.1f;
            if (arrowType == ArrowType::FireArrow)
            {
                num = 0.5f;
            }
            if (arrowType == ArrowType::WoodenArrow)
            {
                num = 0.2f;
            }
            if (arrowType == ArrowType::DiamondArrow)
            {
                num = 0.0f;
            }
            if (arrowType == ArrowType::IronBolt)
            {
                num = 0.05f;
            }
            if (arrowType == ArrowType::DiamondBolt)
            {
                num = 0.0f;
            }
            if (GameSingleton::gameRandom->getFloat(0.0f, 1.0f) < num)
            {
                return true;
            }
        }
        return false;
    }

    void SubsystemArrowBlockBehavior::load(const nlohmann::json &json) {}

    static const std::string subsystemName = "ArrowBlockBehavior";
    const std::string &SubsystemArrowBlockBehavior::getName() const
    {
        return subsystemName;
    }
}
